package cn.fastoo.common.sampleframe.dto;

import cn.fastoo.common.sampleframe.search.QuerySearch;
import cn.fastoo.common.sampleframe.springside.Collections3;
import cn.fastoo.common.sampleframe.utils.ExcelUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializerFeature;
import cn.fastoo.common.sampleframe.search.QuerySearch;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

public class JsonResult implements Serializable{
	
	private int code;
	private String msg;
	private Object data ;
	
	public JsonResult(int code, String msg, Object data){
		this.setCode(code);
		this.setMsg(msg);
		this.setData(data) ;
	}
	public JsonResult(int code,String msg){
		this.setCode(code);
		this.setMsg(msg);
		this.setData(null);
	}

	 
	public static String create(int code, String msg, Object data){
		return new JsonResult(code, msg, data).toJson();
	}
	public static String create(int code, String msg){
		return new JsonResult(code, msg, null).toJson();
	}
 
	
	public static String error(String msg, Object data){
		return new JsonResult(-1,msg,data).toJson();
	}
	public static String error(String msg){
		return new JsonResult(-1,msg,"").toJson();
	}
 
	public static String success(String msg, Object data){
		return new JsonResult(0,msg,data).toJson();
	}
	public static String success(String msg){
		return new JsonResult(0,msg,"").toJson();
	}
	public static String success(Object data){
			return new JsonResult(0,"",data).toJson();
	}
	public static String success(Object data, SerializerFeature serializerFeature){
		return new JsonResult(0,"",data).toJson(serializerFeature);
	}
	public static <T> String querySearchSuccess(List<T> data, QuerySearch querySearch){
		if(querySearch.getFields()!=null){
			querySearch.getResponse().setContentType("application/binary;charset=ISO8859_1");
			try{
				querySearch.getResponse().setHeader("Content-disposition", "attachment; filename=" + new String(querySearch.getFileName().getBytes(), "ISO8859_1") + ".csv");// 组装附件名称和格式
				if(Collections3.isNotEmpty(data)){
					ExcelUtil.export(data.get(0).getClass() , data, querySearch.getTitle().split(","),querySearch.getFields().split(","),querySearch.getResponse().getOutputStream());
				}
	        }
	        catch (IOException e)
	        {
	            e.printStackTrace();
	        }
		}
		return success(data);
	}

	public static <T> String querySearchSuccess(Page<T> page, QuerySearch querySearch){
		if(querySearch.getFields()!=null){
			List<T> data = page.getContent();
			querySearch.getResponse().setContentType("application/binary;charset=ISO8859_1");
			try{
				querySearch.getResponse().setHeader("Content-disposition", "attachment; filename=" + new String(querySearch.getFileName().getBytes(), "ISO8859_1") + ".csv");// 组装附件名称和格式
				if(Collections3.isNotEmpty(data)){
					ExcelUtil.export(data.get(0).getClass() , data, querySearch.getTitle().split(","),querySearch.getFields().split(","),querySearch.getResponse().getOutputStream());
				}
	        }
	        catch (IOException e)
	        {
	            e.printStackTrace();
	        }
		}
		return success(page);
	}

	@JSONField(serialize=false)
	public boolean isSuccess(){
		return this.code==0;
	}

	@JSONField(serialize=false)
	public boolean isError(){
		return this.code!=0;
	}
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public Object getData() {
		return data;
	}
	@JSONField(serialize=false)
	public JSONObject getJData(){
		return JSONObject.parseObject(data.toString());
	}
	public void setData(Object data) {
		if(data ==null){
			data = "";
		}
		this.data = data;
	}
	
	public String toJson() {
		 return JSON.toJSONString(this,SerializerFeature.DisableCircularReferenceDetect);
	}
	public String toJson(SerializerFeature serializerFeature) {
		 return JSON.toJSONString(this,serializerFeature);
	}

}
